package cn.jinjimi.app.main.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.baiiu.filter.util.UIUtil;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.base.util.ViewUtils;
import cn.jinjimi.app.R;

/**
 * Created by lforxeverc on 2017/5/4.
 */

public class SingleLineElipseTextView extends View {
    private String text;
    private String drawText;
    private TextPaint paint;
    private int maxWidth;
    private String DEFAULT = "...";
    private String END = " 分享过";
    private int textSize;
    private int fontSpace;
    private String time, name;

    public SingleLineElipseTextView(Context context) {
        this(context, null);
    }

    public SingleLineElipseTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SingleLineElipseTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        textSize = UIUtil.dp(context, 12);
        paint = new TextPaint();
        paint.setAntiAlias(true);
        paint.setTextSize(textSize);
        paint.setColor(ContextCompat.getColor(getContext(), R.color.text_light_gray));
        fontSpace = getFontSpace();

    }

    public void setTextSize(int textSize) {
        this.textSize = ViewUtils.sp2px(getContext(), textSize);
        paint.setTextSize(this.textSize);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        Paint.FontMetrics fm = paint.getFontMetrics();
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), (int) (Math.abs(fm.descent - fm.ascent) + 0.5));
        if (maxWidth == 0) {
            maxWidth = getMeasuredWidth();
        }
    }

    public void setText(String time, String name) {
        this.name = name;
        this.time = time;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //只显示时间
        if (StringUtils.isEmpty(name) && StringUtils.isNotEmpty(time)) {
            setVisibility(VISIBLE);
            drawText = time;
            canvas.drawText(drawText, 0, textSize / 2 + fontSpace, paint);
            return;
        }
        //都为空不显示
        if (StringUtils.isEmpty(time) && StringUtils.isEmpty(name)) {
            setVisibility(INVISIBLE);
            return;
        }
        setVisibility(VISIBLE);
        String allText = time + " " + name + END;
        if (allText.equals(text) && StringUtils.isNotEmpty(drawText)) {
            canvas.drawText(drawText, 0, textSize / 2 + fontSpace, paint);
            return;
        }
        text = allText;
        float length = paint.measureText(allText, 0, allText.length());
        if (length < maxWidth && StringUtils.isNotEmpty(allText)) {
            drawText = allText;
            canvas.drawText(drawText, 0, textSize / 2 + fontSpace, paint);
        } else {
            float timeLength = paint.measureText(time + " ");
            float shareLength = paint.measureText(END);
            float defaultLength = paint.measureText(DEFAULT);

            float theRest = maxWidth - timeLength - shareLength - defaultLength;
            if (theRest < 0 && StringUtils.isNotEmpty(time)) {
                drawText = time;
                canvas.drawText(drawText, 0, textSize / 2 + fontSpace, paint);
                return;
            }

            for (int i = name.length(); i > 0; i--) {
                float l = paint.measureText(name.substring(0, i));
                if (l < theRest) {
                    drawText = time + " " + name.substring(0, i) + DEFAULT + END;
                    canvas.drawText(drawText, 0, textSize / 2 + fontSpace, paint);
                    break;
                }
            }
        }

    }

    private int getFontSpace() {
        Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
        return (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent + UIUtil.dp(getContext(), 1);

    }
}
